<?php
// 个人秀(晒单)
class Controller_App_Show extends Controller_App_Template
{
    public function __construct ($req, $res)
    {
        $this->template = "smarty:app/show/create";
        parent::__construct($req, $res);
    }

    // 个人秀默认入口
    // @param   string type = <all|follow>
    public function action_index ($type = 'all', $page = 1)
    {
        $page  = max($page, 1);
        $limit = 10;
        $start = $limit * ($page - 1);

        if ($type == 'follow')
        {
            $this->userShouldLogin();
            $items = $this->_show->follow($start, $limit);
        }
        else
            $items = $this->_show->recommends($start, $limit);

        $this->template->set_filename('smarty:app/show/default');
        $this->template->set('showType', $type);
        $this->template->set('items', $items);
        
        $title_json  = App_Util::buildClientTitleJson('search', 'none', '发现', 'message', 'none', '');
        $bottom_json = App_Util::buildClientBottomJson('block', 'show');
        
        $this->template->set('title_json',   $title_json);
        $this->template->set('bottom_json',  $bottom_json);
    }

    // 买家个人秀主页
    public function action_user ($userUid = NULL)
    {
        if($userUid == $this->_uid)
            $this->request->redirect ('/user/show/'.$userUid);
        
        $userUid = $userUid > 0 ? $userUid : $this->_uid;
        $iUser   = User::getUserFromUid($userUid);
        $account = App_Account::account($iUser);
        $iBuyer  = $iUser->getUserInfo();
        $follow  = new App_Follow($this->_uid);
        $iBuyer['hasFollowed'] = $follow->setGroupType('user')->hasFollowUser($userUid);

        $alluserinfo  = $iUser->getUserConfigInfo();
        $this->template->set('allUserInfo', $alluserinfo);

        $this->template->set('items', $this->_show->user($userUid));
        $this->template->set('userinfo', $iBuyer);
        $this->template->set('account', $account);
        $this->template->set_filename('smarty:app/show/user');
        $this->template->set('is_other_user', 1);
        
        $title_json  = App_Util::buildClientTitleJson('back', 'block', '买家个人秀', 'message', 'none', '');
        $bottom_json = App_Util::buildClientBottomJson('none', 'homepage');
        
        $this->template->set('title_json',   $title_json);
        $this->template->set('bottom_json',  $bottom_json);
        
        $randomNum = rand(1, 10);
        $this->template->set('rand', $randomNum);
    }
   
    // 买家订单的个人秀详情
    public function action_detail ($nid)
    {
        $detail = $this->_show->detail($nid);
        $iBuyer = Arr::get($detail, 'buyer');

        $follow  = new App_Follow($this->_uid);
        $detail['hasFollowed'] = $follow->setGroupType('user')->hasFollowUser($iBuyer['userUid']);

        $this->template->set('shareTitle', App_Util::buildShareContent($detail['goods']['name']));
        $this->template->set('shareType', 'show_detail');

        $this->template->set_filename('smarty:app/show/detail');
        $this->template->set('detail', $detail);
        $this->template->set('userinfo', $iBuyer);
        
        if(empty($detail['comments'])){
            $comment = '暂无评论';
        }else{
            $comment = mb_substr($detail['comments'], 0, 10, 'utf-8');
        }
        $this->template->set('comment', $comment);
        
        $title_json  = App_Util::buildClientTitleJson('back', 'block', '买家个人秀', 'share', 'block', '');
        $bottom_json = App_Util::buildClientBottomJson('none', 'homepage');
        
        $this->template->set('title_json',   $title_json);
        $this->template->set('bottom_json',  $bottom_json);
    }

    // 创建晒单
    public function action_create ($orderNo)
    {
        if (! App_Order::userHasOrder($this->_uid, $orderNo))
            throw new Http_Exception_404();

        if (! empty($this->_post))
        {
            $comments = Arr::get($this->_post, 'comments');
            $images   = Arr::get($this->_post, 'images');

            $showId = $this->_show->setOrderShow($orderNo, $comments, $images);
            $this->request->redirect('/show/detail/'. $showId);
        }
        else
        {
            $showId = $this->_show->getOrderShow($orderNo);
            if ($showId > 0)
            {
                $this->request->redirect('/show/detail/'. $showId);
            }
            else
            {
                //
            }

        } // _post
    }

    // 发布晒单
    public function action_release ()
    {
        if(!empty($_GET)){
            Kohana::$log->add(Log::DEBUG, "GET::: get:".  json_encode($_GET));
        }else{
            exit;
        }
        $this->template->set('uid',   $_GET['uid']);
        $this->template->set('orderId',   $_GET['orderId']);
        $this->template->set('orderNo',   $_GET['orderNo']);
        $this->template->set('imagepath',   $_GET['imageUrl']);
        $this->template->set_filename('smarty:app/show/release');
        
        $title_json  = App_Util::buildClientTitleJson('back', 'block', '晒单', 'share', 'none', '');
        $bottom_json = App_Util::buildClientBottomJson('none', 'homepage');
        
        $this->template->set('title_json',   $title_json);
        $this->template->set('bottom_json',  $bottom_json);
    }
    
    public function action_release_post()
    {
        Kohana::$log->add(Log::DEBUG, "GET::: get:".  json_encode($_POST));
//        var_dump($_POST);exit;
         $this->_show->setOrderShow($_POST['orderno'], $_POST['comments'], $_POST['imagepath'], $_POST['imagepath']);
         $this->request->redirect('/user/show');
    }


    public function before ()
    {
        parent::before();
        $this->_show = new App_Show($this->_uid);
    }
 
}
